package com.service.uai;

import java.io.Reader;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.bpms.common.CommonUtil;
import com.bpms.common.VO;
import com.bpms.esb.IService;

public class Parsehdprintxml implements IService {

	public VO exec(VO args) {
		return getXML(args.getAttr("message"));
	}
	/**
	 * get a xml, and decode to a vo object (use jDom)
	 * 
	 * @param bytes
	 * @return
	 */
	public VO getXML(String xml) {
		VO vo = new VO();
		SAXBuilder sb = new SAXBuilder();
		Document doc = null;
		Reader in = new StringReader(xml);

		try {
			doc = sb.build(in);
			vo=parseXml(doc);
			
		} catch (Exception e) {
			CommonUtil.debug("Parsehdprintxml.getXML exception", e);
		}
		return vo;
	}
	
	public VO parseXml(Document document) {
		VO vo = new VO();
		Element root = document.getRootElement();
		Element edecl = (Element) root.getChildren("gDecl").get(0);
		Element cMsgType = (Element) edecl.getChildren().get(0);
		vo.setAttr(cMsgType.getName(), cMsgType.getValue());
		List body = ((Element) (root.getChildren("gTxData").get(0)))
				.getChildren();
		for (Iterator it = body.iterator(); it.hasNext();) {
			Element el = (Element) it.next();
			vo.setAttr(el.getName(), el.getValue());
		}
		return vo;
	}
}
